package components.player
{
	import UIFree.uif_Timer;
	import UIFree.uif_component;
	
	import UIUtils.uif_util;
	
	import components.c_window;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	
	public class c_playerWindow extends c_window {
		public static var PLAYERAPIURL: String = 'http://www.youtube.com/apiplayer?version=3';
		private var _player: Object;
		public function c_playerWindow(parent:uif_component, a_title:String='', modal:Boolean=false, canClose:int=2) {
			super(parent, 300, 200, a_title, modal, canClose);
		}
		
		override protected function createDialogComponents():void {
			super.createDialogComponents();
			createPlayer();
		}
		
		protected function createPanel(): void {
			new c_playerPanel(this, app.styles.playPanel);
		}
		
		protected function createPlayer(): void {
			// This will hold the API player instance once it is initialized.

			var loader: Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
			loader.load(new URLRequest(PLAYERAPIURL));
			
			function onLoaderInit(event: Event):void {
				addChild(loader);
				loader.content.addEventListener(GooglePlayerEvent.READY, onPlayerReady);
				loader.content.addEventListener(GooglePlayerEvent.ERROR, onPlayerError);
				loader.content.addEventListener(GooglePlayerEvent.STATECHAGE, onPlayerStateChange);
				loader.content.addEventListener(GooglePlayerEvent.QUALITYCHANGE, onVideoPlaybackQualityChange);
			}
			
			function onPlayerReady(event: Event):void {
//				trace("player ready:",Object(event).data);
				doSetPlayer(loader.content);
				dispatchEvent(event);
			}
			
			function onPlayerError(event:Event):void {
//				trace("player error:", Object(event).data);
				dispatchEvent(event);
			}
			
			function onPlayerStateChange(event: Event):void {
//				trace("player state:", Object(event).data);
				if (Object(event).data == 1)
					dispatchEvent(new Event(Event.COMPLETE));
				dispatchEvent(event);
			}
			
			function onVideoPlaybackQualityChange(event: Event):void {
				var space: Number = getSpace(true);
				var inner: Rectangle = getInner();
				loader.x = inner.x;
				loader.y = inner.y + title.height + space;
				resize(loader.width + space * 2, loader.height + space * 3 + title.height + c_playerPanel.HEIGHT + space);
			}
		}
		
		override protected function doClose(e:Event):void {
			super.doClose(e);
			if (player) player.stopVideo();
		}
		
		protected function doSetPlayer(aPlayer: Object): void {
			_player = aPlayer;
			createPanel();
		}
		
		public function get player(): Object {
			return _player;
		}
		
		public function load(viewURL: String, onComplete: Function=null): void {
			uif_util.waitCall(function(): Boolean {
				return player != null;
			}, function(): void {
				if (onComplete != null) {
					function l_doComplete(e: Event): void {
						removeEventListener(Event.COMPLETE, l_doComplete);
						onComplete();
						uif_Timer.one(1000, function(e: Event): void {
							_player.setPlaybackQuality(GooglePlayerQuality.LARGE);
						});
					}
					addEvent(Event.COMPLETE, l_doComplete);
				}
				player.loadVideoByUrl(viewURL);
			});
		}
	}
}